<?php


namespace app\service;


use app\entity\AddressEntity;
use app\entity\DzclEntity;
use app\exception\ErrorCode;
use app\exception\OaException;
use app\models\request\ReqActListPage;
use app\models\request\ReqAddAddress;
use app\models\request\ReqId;
use app\models\response\RespAddressDetail;
use app\models\response\RespPage;
use yii\data\Pagination;

class DzclService
{



    /**
     * @param int $userId
     * @return RespPage
     */
    public function listPage(ReqActListPage $req) {
        $data = DzclEntity::find()->where(["del_flag"=>0])->orderBy("id desc");

        if ($req->getGameId()) {
            $data->andWhere(["game_id"=>$req->getGameId()]);
        }
        if ($req->getType()) {
            $data->andWhere(["type"=>$req->getType()]);
        }

        $pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => $req->getPageSize(),'page'=>$req->getPageNo()-1]);
        /**@var $entities DzclEntity[]*/
        $entities = $data->offset($pages->getOffset())->limit($pages->getLimit())->all();
        $list = [];
        foreach ($entities as $entity) {

            array_push($list,[
                "id"=>$entity->getId(),
                "title"=>$entity->getTitle(),
                "url"=>$entity->getUrl(),
                "material_info"=>json_decode($entity->getMaterialInfo())
            ]);
        }

        $resp = new RespPage();
        $resp->setTotal(intval($pages->totalCount));
        $resp->setPageSize($pages->getPageSize());
        $resp->setPageNo($req->getPageNo());
        $resp->setList($list);
        return $resp;
    }
}